package com.example.zhunongzonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhunongzonghe.beans.ZnExchangePostBean;
import com.example.zhunongzonghe.dao.ZnExchangePostMapper;
import com.example.zhunongzonghe.entity.ZnExchangeComment;
import com.example.zhunongzonghe.entity.ZnExchangePost;
import com.example.zhunongzonghe.entity.ZnUsers;
import com.example.zhunongzonghe.service.*;
import com.example.zhunongzonghe.utils.RedisCache;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class ZnExchangePostServiceImpl extends ServiceImpl<ZnExchangePostMapper, ZnExchangePost> implements IZnExchangePostService {

    @Autowired
    ZnExchangePostMapper znExchangePostMapper;
    @Autowired
    IZnExchangeCommentService znExchangeCommentService;
    @Autowired
    ZnPostLikeService znPostLikeService;
    @Resource
    ZnCollectPostService znCollectPostService;
    @Resource
    IZnUsersService IZnUsersService;
    @Resource
    private RedisCache redisCache;

    private static final String POST_LIKE_KEY = "post_like";//hash前缀
    private static final String POST_LIKE_COUNT_KEY = "post_like_count";//hash前缀
    private static final String POST_LIKE_DETAIL_KEY = "post_like_detail";//hash前缀
    private static final String POST_COLLECT_KEY = "post_collect";//hash前缀
    private static final String POST_COLLECT_COUNT_KEY = "post_collect_count";//收藏总数前缀
    private static final String POST_COLLECT_DETAIL_KEY = "post_collect_detail";//收藏状态前缀


    /**
     * 分页查询全部帖子
     * @param currentPage
     * @param pageNum
     * @return
     */
    @Override
    public Page<ZnExchangePostBean> searchPostAll(Integer currentPage, Integer pageNum) {
        Page<ZnExchangePost> postPage = new Page<>(currentPage,pageNum);
        Page<ZnExchangePost> exchangePostPage = znExchangePostMapper.selectPage(postPage,null);
        List<ZnExchangePostBean> beanList = new ArrayList<>();
        for (ZnExchangePost znpost: exchangePostPage.getRecords()
             ) {
            ZnExchangePostBean postBean = new ZnExchangePostBean();
            ZnUsers usersBean = IZnUsersService.getById(znpost.getUserId());
            postBean.setNickName(usersBean.getNickname());
            postBean.setAvatar(usersBean.getAvatar());
            postBean.setCommentNumBer(znExchangeCommentService.CommentNumBer(znpost.getPostId()));
            postBean.setLikeCount(znPostLikeService.likeCounts(znpost.getPostId()));
            postBean.setCollectCount(znCollectPostService.CollectCounts(znpost.getPostId()));
            BeanUtils.copyProperties(znpost,postBean);
            beanList.add(postBean);
        }
        Page<ZnExchangePostBean> beanPage = new Page<>(currentPage,pageNum, exchangePostPage.getTotal());
        beanPage.setRecords(beanList);
        return beanPage;
    }

    @Override
    public Page<ZnExchangePostBean> postAll(Integer currentPage, Integer pageNum) {
        Page<ZnExchangePost> postPage = new Page<>(currentPage,pageNum);
        LambdaQueryWrapper<ZnExchangePost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnExchangePost::getStatus,1);
        Page<ZnExchangePost> exchangePostPage = znExchangePostMapper.selectPage(postPage,queryWrapper);
        List<ZnExchangePostBean> beanList = new ArrayList<>();
        for (ZnExchangePost znpost: exchangePostPage.getRecords()
        ) {
            ZnExchangePostBean postBean = new ZnExchangePostBean();
            ZnUsers usersBean = IZnUsersService.getById(znpost.getUserId());
            postBean.setNickName(usersBean.getNickname());
            postBean.setAvatar(usersBean.getAvatar());
            postBean.setCommentNumBer(znExchangeCommentService.CommentNumBer(znpost.getPostId()));
            postBean.setLikeCount(znPostLikeService.likeCounts(znpost.getPostId()));
            postBean.setCollectCount(znCollectPostService.CollectCounts(znpost.getPostId()));
            BeanUtils.copyProperties(znpost,postBean);
            beanList.add(postBean);
        }
        Page<ZnExchangePostBean> beanPage = new Page<>(currentPage,pageNum, exchangePostPage.getTotal());
        beanPage.setRecords(beanList);
        return beanPage;
    }

    @Override
    public Page<ZnExchangePostBean> LogUserPostAll(Integer currentPage, Integer pageNum, String userName) {
        Page<ZnExchangePost> postPage = new Page<>(currentPage,pageNum);
        LambdaQueryWrapper<ZnExchangePost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnExchangePost::getStatus,1);
        Page<ZnExchangePost> exchangePostPage = znExchangePostMapper.selectPage(postPage,queryWrapper);
        ZnUsers znUsers = IZnUsersService.getUserName(userName);
        List<ZnExchangePostBean> beanList = new ArrayList<>();
        for (ZnExchangePost znpost: exchangePostPage.getRecords()
        ) {
            ZnExchangePostBean postBean = new ZnExchangePostBean();
            ZnUsers usersBean = IZnUsersService.getById(znpost.getUserId());
            postBean.setNickName(usersBean.getNickname());
            postBean.setAvatar(usersBean.getAvatar());
            postBean.setCommentNumBer(znExchangeCommentService.CommentNumBer(znpost.getPostId()));
            postBean.setLinked(znPostLikeService.isLiked(znpost.getPostId(),znUsers.getId()));
            postBean.setLikeCount(znPostLikeService.likeCounts(znpost.getPostId()));
            postBean.setCollectOrNot(znCollectPostService.isCollect(znpost.getPostId(),znUsers.getId()));
            postBean.setCollectCount(znCollectPostService.CollectCounts(znpost.getPostId()));
            BeanUtils.copyProperties(znpost,postBean);
            beanList.add(postBean);
        }
        Page<ZnExchangePostBean> beanPage = new Page<>(currentPage,pageNum, exchangePostPage.getTotal());
        beanPage.setRecords(beanList);
        return beanPage;
    }

    /**
     * 分页搜索
     * @param searchTitle
     * @param currentPage
     * @param pageNum
     * @return
     */
    @Override
    public Page<ZnExchangePostBean> searchPostsTitle(Object searchTitle, Integer currentPage, Integer pageNum) {
        Page<ZnExchangePost> beanPage = new Page<>(currentPage,pageNum);
        LambdaQueryWrapper<ZnExchangePost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnExchangePost::getPostName,searchTitle);
        Page<ZnExchangePost> selectPage = znExchangePostMapper.selectPage(beanPage,queryWrapper);
        List<ZnExchangePostBean> postBeans = new ArrayList<>();
        for (ZnExchangePost post: selectPage.getRecords()
             ) {
            ZnUsers znUsers = IZnUsersService.getById(post.getUserId());
            ZnExchangePostBean znExchangePostBean = new ZnExchangePostBean();
            BeanUtils.copyProperties(post,znExchangePostBean);
            znExchangePostBean.setNickName(znUsers.getNickname());
            postBeans.add(znExchangePostBean);
        }
        Page<ZnExchangePostBean> postBeanPage = new Page<>(currentPage,pageNum,selectPage.getTotal());
        postBeanPage.setRecords(postBeans);
        return postBeanPage;
    }

    /**
     * 当前登录的用户查看自己发布的帖子
     * @param currentPage
     * @param pageSize
     * @param userName
     * @return
     */
    @Override
    public Page<ZnExchangePostBean> searchMyPost(Integer currentPage, Integer pageSize, String userName) {
        ZnUsers znUsers = IZnUsersService.getUserName(userName);
        Page<ZnExchangePost> postPage = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<ZnExchangePost> queryPost = new LambdaQueryWrapper<>();
        queryPost.eq(ZnExchangePost::getUserId,znUsers.getId());
        Page<ZnExchangePost> PostPage = znExchangePostMapper.selectPage(postPage,queryPost);
        List<ZnExchangePostBean> beanList = new ArrayList<>();
        for (ZnExchangePost znPost: PostPage.getRecords()
        ) {
            ZnExchangePostBean postBean = new ZnExchangePostBean();
            ZnUsers usersBean = IZnUsersService.getById(znPost.getUserId());
            postBean.setNickName(usersBean.getNickname());
            postBean.setAvatar(usersBean.getAvatar());
            postBean.setCommentNumBer(znExchangeCommentService.CommentNumBer(znPost.getPostId()));
            postBean.setLinked(znPostLikeService.isLiked(znPost.getPostId(),znUsers.getId()));
            postBean.setLikeCount(znPostLikeService.likeCounts(znPost.getPostId()));
            postBean.setCollectOrNot(znCollectPostService.isCollect(znPost.getPostId(),znUsers.getId()));
            postBean.setCollectCount(znCollectPostService.CollectCounts(znPost.getPostId()));
            BeanUtils.copyProperties(znPost,postBean);
            beanList.add(postBean);
        }
        Page<ZnExchangePostBean> beanPage = new Page<>(currentPage,pageSize, PostPage.getTotal());
        beanPage.setRecords(beanList);
        return beanPage;
    }

    @Override
    public void delMyPost(Integer postId, String userName) {
        ZnUsers znUsers = IZnUsersService.getUserName(userName);
        String detailKey = POST_LIKE_DETAIL_KEY+"."+postId+"."+znUsers.getId();
        String countKey = POST_LIKE_COUNT_KEY+"."+postId;
        String detailColl = POST_COLLECT_DETAIL_KEY+ "."+postId+"."+znUsers.getId();
        String countColl = POST_COLLECT_COUNT_KEY+"."+postId;
        redisCache.delCacheHashValue(POST_LIKE_KEY,postId+":"+znUsers.getId());
        redisCache.deleteObject(detailKey);
        redisCache.deleteObject(countKey);
        redisCache.delCacheHashValue(POST_COLLECT_KEY,postId+":"+znUsers.getId());
        redisCache.deleteObject(detailColl);
        redisCache.deleteObject(countColl);
        LambdaQueryWrapper<ZnExchangePost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnExchangePost::getPostId,postId).eq(ZnExchangePost::getUserId,znUsers.getId());
        znExchangePostMapper.delete(queryWrapper);
        LambdaQueryWrapper<ZnExchangeComment> commentQuery= new LambdaQueryWrapper<>();
        commentQuery.eq(ZnExchangeComment::getPostId,postId);
        List<ZnExchangeComment> commentList = znExchangeCommentService.list(commentQuery);
        znExchangeCommentService.removeByIds(commentList);
    }
}
